#ifndef __RN8302B_C
#define __RN8302B_C

// 芯片官网： https://www.renergy-me.com/

#include "rn8302B.h"

//////////// 读数据变量 /////////////////
// 检验和变量
uint8_t	  rnCheckTimes	    = 0; // 校验和读取并校对的次数；次数 > 5; 超过一定数据后，表明rn8209设备有错误失效。
uint16_t	rnCheckSUM	      = 0; //检验和:0x00-0x10寄存器需要检验
uint16_t	rnCheckSUMRead	  = 0xEE79; //rn8209中读取到的校验和寄存器;默认值=0xEE79。
uint32_t  rnDeviceID        = 0;
uint32_t  rnDeviceVerID     = 0;
uint8_t   rnIF              = 0; //记录电表事件

// 向RN8209中的寄存器写入数据：
void RN_WriteDatas(u16 Addr,uint8_t *datas,uint8_t datalen){		
	  uint8_t i = 0;
	  uint8_t CheckSumR = 0;	
	  uint8_t addrTemp  = 0;  
		// 写命令的最高位 = 1;   
	  SPI_bit_reset(SPI_CS_PinId);
    // 延时
	  #ifdef UsingFreeRTOS
	  vTaskDelay(1);
	  #else
	  delaynus(1);	
	  #endif 
    // 开始传数据：地址字节
	  addrTemp = (uint8_t)(Addr & 0x00FF);     
	  CheckSumR = addrTemp;      
		SPI_SendRead_Byte(addrTemp);	
    // 传数据：CMD字节  
	  addrTemp = (((uint8_t)(Addr >> 4)) & 0xf0) + 0x80;
	  CheckSumR += addrTemp;    
	  SPI_SendRead_Byte(addrTemp); 
	  // 传数据：数据字节
		for(i = 0; i < datalen; i++){     
			  SPI_SendRead_Byte(datas[i]); 
			  CheckSumR += datas[i];
		}
		// 传数据：检验和字节
		CheckSumR = ~CheckSumR; 
	  SPI_SendRead_Byte(CheckSumR); 
		// 拉高CS
		SPI_bit_set(SPI_CS_PinId);
		// 延时
	  #ifdef UsingFreeRTOS
	  vTaskDelay(1);
	  #else
	  delaynus(1);	
	  #endif  
}

// 读取RN8209中的数据
u32 RN_ReadDatas(u16 Addr,uint8_t datalen){
	  uint8_t i = 0;	
	  uint8_t CheckSumR = 0;	
	  uint8_t daTemp  = 0;
    u32 retData       = 0;  
		// 读数据的地址最高位=0
	  SPI_bit_reset(SPI_CS_PinId);
	  // 延时
	  #ifdef UsingFreeRTOS
	  vTaskDelay(1);
	  #else
	  delaynus(1);	
	  #endif 
    // 开始传数据：	地址字节
    daTemp = (uint8_t)(Addr & 0x00FF);     
	  CheckSumR = daTemp;
		SPI_SendRead_Byte(daTemp);
	  // 传数据：	命令字节
    daTemp = (((uint8_t)(Addr >> 4)) & 0xf0);     
	  CheckSumR += daTemp;	  	
	  SPI_SendRead_Byte(daTemp);		
	  // 读取数据
	  for(i = 0; i < datalen; i++){
			  daTemp = SPI_SendRead_Byte(0x00);			  
			  retData <<= 8;
			  retData += daTemp;
        CheckSumR += daTemp;	
		}
		// 校验数据有效性
		CheckSumR = ~CheckSumR; 		
		if (CheckSumR != SPI_SendRead_Byte(0x00)){
			  retData = 0xFFFFFFFF;
			
			  #ifdef SYS_DEBUG
			  printf(" RN_Read Error \r\n");
			  #endif
	  }
		// 拉高CS
	  SPI_bit_set(SPI_CS_PinId);
		// 延时
	  #ifdef UsingFreeRTOS
	  vTaskDelay(1);
	  #else
	  delaynus(1);	
	  #endif 
    return retData;    
}

// 初始化校表设置
void RN_Set_Regulation(){
    uint8_t   writeDataRN[3] = {0x00};
		// 开始写寄存器操作
		writeDataRN[0] = 0xE5; 
		RN_WriteDatas(WREN,writeDataRN,WREN_Len);       //使能写操作
		
		writeDataRN[0] = 0xFA;                           
		RN_WriteDatas(SOFTRST,writeDataRN,SOFTRST_Len);  // 复位	
	  delayn50us(1);
		
		writeDataRN[0] = 0xE5; 
		RN_WriteDatas(WREN,writeDataRN,WREN_Len);       //使能写操作
		writeDataRN[0] = 0xA2;
    RN_WriteDatas(WMSW,writeDataRN,WMSW_Len);       //工作模式EMM
    		
		writeDataRN[0] = 0x04; 
		writeDataRN[1] = 0x32; 
		writeDataRN[2] = 0x10; 
		RN_WriteDatas(CFCFG,writeDataRN,CFCFG_Len);     //CF引脚配置寄存器CFCFG,默认值：0x043210
		writeDataRN[0] = 0x40; 
		writeDataRN[1] = 0x00; 
		writeDataRN[2] = 0x00; 
		RN_WriteDatas(EMUCFG, writeDataRN, EMUCFG_Len);   //计量单元配置寄存器，默认值：0x400000
		writeDataRN[0] = 0x77; 
		writeDataRN[1] = 0x77; 
		writeDataRN[2] = 0x77;
		RN_WriteDatas(EMUCON, writeDataRN, EMUCON_Len);   //使能该相RMS基波电能视在计量；使能基波无功电能计量；使能该相基波有功电能计量；使能A/B/C相视在电能计量；使能ABC相无功计量；ABC相有功计量使能
		writeDataRN[0] = 0x00; 
		writeDataRN[1] = 0x00; 
		RN_WriteDatas(ADCCFG, writeDataRN ,ADCCFG_Len);    //IA/IB/IC/IN/UA/UB/UC通道模拟增益为1倍增益
		writeDataRN[0] = 0x00; 
		RN_WriteDatas(MODSEL, writeDataRN, MODSEL_Len);   //切换到三相四线制
		writeDataRN[0] = 0x10; 		
		RN_WriteDatas(WSAVECON, writeDataRN,WSAVECON_Len);  //清空采样数据缓存区，配置该命令20ms后，整个BUF清零。	  
		writeDataRN[0] = 0x00; 
		writeDataRN[1] = 0x73; 
		RN_WriteDatas(ZXOT, writeDataRN, ZXOT_Len);    //设置过零阈值寄存器，16位无符号数
    writeDataRN[0] = 0x00; 
		writeDataRN[1] = 0x00; 
		RN_WriteDatas(LostVoltT, writeDataRN, LostVoltT_Len);	//设置失压阈值寄存器，16位无符号数
    writeDataRN[0] = 0x00; 
		writeDataRN[1] = 0x00; 
		writeDataRN[2] = 0x00;
		RN_WriteDatas(SAGCFG, writeDataRN, SAGCFG_Len);     //设置电压暂降阈值寄存器，3字节，第3个字节为电压暂降时间阈值；第1/2字节为电压暂降峰值阈值
    writeDataRN[0] = 0x00; 
		writeDataRN[1] = 0x00; 
		RN_WriteDatas(OVLVL, writeDataRN,OVLVL_Len);
    writeDataRN[0] = 0x00; 
		writeDataRN[1] = 0x00; 
		RN_WriteDatas(OILVL, writeDataRN,OILVL_Len);
    writeDataRN[0] = 0xDC; 		
		RN_WriteDatas(WREN, writeDataRN,WREN_Len);  //写保护
		
}



#endif

